package com.tanhua.app.controller;

import com.tanhua.app.Service.SettingsService;
import com.tanhua.app.Service.UserInfoService;
import com.tanhua.app.Service.UserService;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;

    //    用户资料 - 读取
    //    GET   /users
    @GetMapping
    public ResponseEntity userInfo(Long id) {
        if (id == null) {
            id = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(id);
        return ResponseEntity.ok(userInfo);
    }

//    用户资料 = 保存
//    PUT/users
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

//    用户通用设置 - 读取
//    GET   /users/settings
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }

//    设置陌生人问题 - 保存
//    POST  /users/questions
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map<String,String> map){
        String content = map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

//    通知设置 - 保存
//    POST  /users/notifications/setting
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

//    黑名单 - 翻页列表
//    GET   /users/blacklist
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<UserInfo> pageResult = settingsService.blacklist(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

//    黑名单 - 移除
//    DELETE    /users/blacklist/:uid
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable("uid") Long uid){
        settingsService.deleteBlackList(uid);
        return ResponseEntity.ok(null);
    }

//    修改手机号- 1 发送短信验证码
//    POST  /users/phone/sendVerificationCode
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        userService.sendCode(UserHolder.getMobile());
        return ResponseEntity.ok(null);
    }

//    修改手机号 - 2 校验验证码
//    POST  /users/phone/checkVerificationCode
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map code) {
        String verificationCode = code.get("verificationCode").toString();
        boolean verification = userService.checkVerification(UserHolder.getMobile(),verificationCode);
        Map<String,Boolean> map = new HashMap<>();
        map.put("verification",verification);
        return ResponseEntity.ok(map);
    }

//    修改手机号 = 3 保存
//    POST  /users/phone
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.phone(phone);
        return ResponseEntity.ok(null);
    }

//    用户资料 - 更新头像
//    POST  /users/header
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws Exception {
        Long id = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }

//    互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
//    GET   /users/friends/:type
    @GetMapping("/friends/{type}")
    public ResponseEntity fansList(@PathVariable("type") String type,
                                   @RequestParam(defaultValue = "1")Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String nickname) {
        PageResult pr = userService.fansList(page,pagesize,type,nickname);
        return ResponseEntity.ok(pr);
    }

//    互相喜欢，喜欢，粉丝 - 统计
//    GET   /users/counts
    @GetMapping("/counts")
    public ResponseEntity counts(){
        LikeCountVo vo = userService.likeCounts();
        return ResponseEntity.ok(vo);
    }

//    喜欢 - 取消喜欢
//    DELETE    /users/like/:uid
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLike(@PathVariable("uid")String likeId){
        userService.deleteLike(likeId);
        return ResponseEntity.ok(null);
    }

//    粉丝 - 喜欢
//    POST  /users/fans/:uid
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid")String likeId){
        userService.fansLike(likeId);
        return ResponseEntity.ok(null);
    }
}

